<template>
    <div>
        <div style="margin-top: 10px;display: flex;justify-content: center">
            <el-input v-model="keywords" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search"
            style="width: 400px;margin-right: 10px" ></el-input>
            <el-button icon="el-icon-search" type="primary" @click="doSearch">搜索</el-button>
        </div>
        <div style="margin-top: 10px;display: flex;justify-content: left">
            <el-button icon="el-icon-plus" type="primary" @click="dialogFormVisible = true">添加用户</el-button>
        </div>
        <div>
            <el-dialog title="用户新增" :visible.sync="dialogFormVisible">
                <el-form :model="form" >
                    <el-col :span="24"  style="display: grid; place-items: center;">
                    <el-form-item label="">
                        <el-upload
                                class="avatar-uploader"
                                action="/plateform/system/hr/data/upload"
                                :headers="getToken()"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="form.name" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="登录名" :label-width="formLabelWidth">
                        <el-input v-model="form.username" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="密码" :label-width="formLabelWidth">
                        <el-input v-model="form.password" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="电话" :label-width="formLabelWidth">
                        <el-input v-model="form.phone" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="座机" :label-width="formLabelWidth">
                        <el-input v-model="form.telephone" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="12">
                    <el-form-item label="地址" :label-width="formLabelWidth">
                        <el-input v-model="form.address" autocomplete="off" style="width: 300px"></el-input>
                    </el-form-item>
                    </el-col>
                    <el-col :span="24">
                    <el-form-item label="备注" :label-width="formLabelWidth">
                        <el-input v-model="form.remark" autocomplete="off" ></el-input>
                    </el-form-item>
                    </el-col>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAddHr">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <div class="hr-container">
            <el-card
                    class="hr-card" v-for="(hr,index) in hrs" :key="index">
                <div slot="header" class="clearfix">
                    <span>{{hr.name}}</span>
                    <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete"
                    @click="deleteHr(hr)"></el-button>
                </div>
                <div>
                    <div class="img-container">
                        <img :src="getImageSrc(hr.userface)" :alt="hr.name" :title="hr.name" class="userface-img">
                    </div>
                    <div class="userinfo-container">
                        <div>用户名：{{hr.name}}</div>
                        <div>手机号码：{{hr.phone}}</div>
                        <div>电话号码：{{hr.telephone}}</div>
                        <div>地址：{{hr.address}}</div>
                        <div>用户状态：
                            <el-switch
                                    v-model="hr.enabled"
                                    active-text="启用"
                                    @change="enabledChange(hr)"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    inactive-text="禁用">
                            </el-switch>
                        </div>
                        <div>
                            用户角色：
                            <el-tag style="margin: 3px"
                                    type="success" v-for="(role,index) in hr.roles" :key="index">{{role.nameZh}}</el-tag>
                            <el-popover
                                    placement="right"
                                    title="角色列表"
                                    @show="showPop(hr)"
                                    @hide="hidePop(hr)"
                                    width="200"
                                    trigger="click">
                                <el-select v-model="selectedRoles" multiple placeholder="请选择">
                                    <el-option
                                            v-for="(r,indexKey) in allRoles"
                                            :key="indexKey"
                                            :label="r.nameZh"
                                            :value="r.id">
                                    </el-option>
                                </el-select>
                                <el-button slot="reference" icon="el-icon-more" type="text"></el-button>
                            </el-popover>


                        </div>
                        <div>
                            备注：{{hr.remark}}
                        </div>
                    </div>
                </div>
            </el-card>

        </div>
    </div>
</template>

<script>
    export default {
        name: "SysHr",
        data(){
            return{
                isMounted: false,
                keywords:'',
                hrs:[],
                selectedRoles:[],
                allRoles:[],
                dialogFormVisible: false,
                form: {
                    name: '',
                    username: '',
                    phone: '',
                    telephone: '',
                    address: '',
                    enabled:1,
                    password:'',
                    userface:'',
                    remark: ''
                },
                imageUrl:'',
                formLabelWidth: '120px',
                token:'',
                filePath:''
            }
        },
        mounted() {
            if(!this.isMounted){
                this.initHrs();
            }
            this.isMounted = true;

        },
        methods:{
            getToken(){
                let str = window.sessionStorage.getItem("token");
                this.token = str;
                const authorization = this.token;
                return { "X-TOKEN":authorization }
            },
            getImageSrc(headUrl){
                if(headUrl.startsWith('http')){
                    return headUrl;
                }
                return this.filePath+"/"+headUrl;

            },
            handleAvatarSuccess(res, file) {
                this.dialogFormVisible = true;
                this.form.userface = res.data;
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            doAddHr(){
                this.postRequest("/security/addHr",this.form).then(resp=>{
                    if(resp){
                        this.dialogFormVisible = false;
                        this.initHrs();
                    }
                })
            },
            deleteHr(hr){
                this.$confirm('此操作将永久删除['+hr.name+'], 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.deleteRequest("/system/hr/"+hr.id).then(resp=>{
                        if(resp){
                            this.initHrs();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doSearch(){
                this.initHrs();
            },
            hidePop(hr){
                let roles =[];
                Object.assign(roles,hr.roles) ;
                let flag=false;
                if(roles.length !=this.selectedRoles.length){
                    flag=true;
                }else{
                    for(let i=0;i<roles.length;i++){
                        let role=roles[i];
                        for(let j=0;j<this.selectedRoles.length;j++){
                            let sr= this.selectedRoles[j];
                            if(role.id==sr){
                                roles.splice(i,1);
                                i--;
                                break;
                            }
                        }
                    }
                    if(roles.length!=0){
                        flag=true;
                    }
                }
                if(flag){
                    let url = "/system/hr/role?hrid="+hr.id;
                    this.selectedRoles.forEach(sr=>{
                        url+='&rids='+sr;
                    })
                    this.putRequest(url).then(resp=>{
                        if (resp){
                            this.initHrs();
                        }
                    })
                }

            },
            showPop(hr){
                this.initAllRoles();
                let roles=hr.roles;
                this.selectedRoles=[];
                roles.forEach(r=>{
                    this.selectedRoles.push(r.id);
                })

            },
            initAllRoles(){
                this.getRequest("/system/hr/roles").then(resp=>{
                    if(resp){
                        this.allRoles=resp;
                    }
                })
            },
            enabledChange(hr){
                delete hr.roles;
                this.putRequest("/system/hr",hr).then(resp=>{
                    if (resp){
                        this.initHrs();
                    }
                })
            },
            initHrs(){
                this.getRequest("/system/hr/?keywords="+this.keywords).then(resp=>{
                    if(resp){
                        this.hrs=resp;
                        this.form.name='';
                        this.form.username='';
                        this.form.phone='';
                        this.form.telephone='';
                        this.form.address='';
                        this.form.enabled=1;
                        this.form.password='';
                        this.form.userface='';
                        this.form.remark='';
                        this.imageUrl='';
                    }
                });
                this.getRequest("/system/hr/getCommonFilePath").then(resp=>{
                    if(resp){
                        this.filePath = resp;
                    }
                })
            }
        }
    }
</script>

<style >
    .hr-container{
        margin-top: 20px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }
    .hr-card{
        width: 350px;
        margin-bottom: 10px;
    }
    .img-container{
        width: 100%;
        display: flex;
        justify-content: center;
    }
    .userface-img{
        width: 72px;
        height: 72px;
        border-radius: 72px;
    }
    .userinfo-container{
        margin-top: 20px;
    }
    .userinfo-container div{
        font-size: 12px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 50%;
        display: inline-block; /* 确保按钮可以正确显示为块级元素 */
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

</style>